package com.fingard.dsp.bank.directbank.boc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransactionReplenish;
import com.fingard.dsp.bank.reqrespobj.transaction.ReqTransactionReplenish;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTransactionReplenish;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTransactionTaxModel;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransactionReplenishDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.*;

public class BOC01TransactionReplenish extends BOC01Base implements ITransactionReplenish {

    public RetAtsTransactionReplenish qryTransactionReplenish(ReqTransactionReplenish reqQryTransactionReplenish) throws Exception {
        RetAtsTransactionReplenish retAts = new RetAtsTransactionReplenish(reqQryTransactionReplenish.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "0";
        String tmpRespInfo = "";
        int qryCount = 0;
        int countNum = 0;
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(reqQryTransactionReplenish.getDateFrom());
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(reqQryTransactionReplenish.getDateTo());
            Calendar tmpClndrStepTo = Calendar.getInstance();
            tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
            BOC01Header bankHeader = new BOC01Header();
            bankHeader.trncod = "b2e0531";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            while (qryCount <= countNum && tmpRespCode.equals("0")) {//分页查询
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("trn-b2e0531-rq");
                xmlWriter.writeStartElement("b2e0531-rq");
                xmlWriter.writeStartElement("multidata");
                xmlWriter.writeStartElement("confirmdata");
                xmlWriter.writeElementString("actacn", reqQryTransactionReplenish.actNum);//查询账号
                xmlWriter.writeElementString("ibknum", "");//联行号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
                xmlWriter.writeElementString("trantype", "A");//A:查询全部
                xmlWriter.writeStartElement("datescope");
                xmlWriter.writeElementString("from", Format.Date8Format.format(tmpCalendarFrom.getTime()));//开始日期
                xmlWriter.writeElementString("to", Format.Date8Format.format(tmpCalendarTo.getTime()));//截至日期
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("amtscope");
                xmlWriter.writeElementString("minamt", "");//开始日期
                xmlWriter.writeElementString("maxamt", "");//截至日期
                xmlWriter.writeEndElement();
                xmlWriter.writeStartElement("domaccount");
                xmlWriter.writeElementString("transact", "");
                xmlWriter.writeElementString("transname", "");
                xmlWriter.writeEndElement();
                xmlWriter.writeElementString("begnum", String.valueOf(qryCount == 0 ? 1 : qryCount));
                xmlWriter.writeElementString("recnum", "10");
                xmlWriter.writeEndDocument();
                xmlWriter.writeEndDocument();
                xmlWriter.writeEndDocument();
                WriteBankLogLn("发送银行报文：" + xmlWriter.toXmlString());
                System.out.println(xmlWriter.toXmlString());
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                //测试
                //String[] tmpStrRet = {"","<?xml version=\"1.0\" encoding=\"GB18030\"?><bocb2e><trans><trn-b2e0531-rs><status><rspcod>B001</rspcod>" +
                //        "<rspmsg></rspmsg></status><totalnum>1</totalnum></trn-b2e0531-rs><trn-b2e0531-rs><b2e0531-rs><status><rspcod>B001</rspcod></status>" +
                //       "<cusid>11111111</cusid><receiptno>2222222</receiptno><bustyp></bustyp><taxcnt>2</taxcnt><taxdate1>2019/07/15</taxdate1><taxname1>测试</taxname1><taxamount1>100</taxamount1>" +
                //       "<taxdate2>2019/07/15</taxdate2><taxname2>测试</taxname2><taxamount2>100</taxamount2></b2e0531-rs></trn-b2e0531-rs></trans>" +
                //       "</bocb2e>"};
                XmlTextReader tmpBkReader;
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0531-rs/status/rspcod");
                        String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0531-rs/status/rspmsg");
                        countNum = Integer.valueOf(tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0531-rs/totalnum"));
                        if (isActiveRetCode(tmprspcod)) {
                            //B028 账户无交易
                            //B003 交易数为0
                            if ("B028".equals(tmprspcod) || "B003".equals(tmprspcod)) {
                                break;
                            }
                            //循环数
                            String eachRspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0531-rs/b2e0531-rs/status/rspcod");
                            if ("B001".equals(eachRspcod)) {
                                while (tmpBkReader.read("/bocb2e/trans/trn-b2e0531-rs/b2e0531-rs/transinfo")) {
                                    RetAtsTransactionReplenishDetail tmpRetDetail = new RetAtsTransactionReplenishDetail();
                                    List<RetAtsTransactionTaxModel> list = new ArrayList<RetAtsTransactionTaxModel>();
                                    tmpRetDetail.cusid = tmpBkReader.readedXElement.getSingleText("cusid");
                                    tmpRetDetail.receiptno = tmpBkReader.readedXElement.getSingleText("receiptno");
                                    tmpRetDetail.bustyp = tmpBkReader.readedXElement.getSingleText("bustyp");
                                    tmpRetDetail.taxno = tmpBkReader.readedXElement.getSingleText("taxno");
                                    tmpRetDetail.taxcnt = tmpBkReader.readedXElement.getSingleText("taxcnt");
                                    tmpRetDetail.taxofficename = tmpBkReader.readedXElement.getSingleText("taxofficename");
                                    tmpRetDetail.taxno = tmpBkReader.readedXElement.getSingleText("taxno");
                                    tmpRetDetail.receiptjrnlno = tmpBkReader.readedXElement.getSingleText("receiptjrnlno");
                                    if (!StringHelper.isNullOrEmpty(tmpRetDetail.taxcnt)) {
                                        int taxcnt = Integer.valueOf(tmpRetDetail.taxcnt.replaceAll("^(0+)", ""));
                                        for (int i = 1; i <= taxcnt; i++) {
                                            RetAtsTransactionTaxModel dto = new RetAtsTransactionTaxModel();
                                            dto.setTaxdate(tmpBkReader.readedXElement.getSingleText("taxdate" + i));
                                            dto.setTaxname(tmpBkReader.readedXElement.getSingleText("taxname" + i));
                                            dto.setTaxamount(tmpBkReader.readedXElement.getSingleText("taxamount" + i));
                                            //不使用MAP存储
                                            /*
                                            ConcurrentHashMap<String,String> map = new ConcurrentHashMap<String,String>();
                                            map.put("dto",tmpBkReader.readedXElement.getSingleText("taxdate"+i));
                                            map.put("Taxname",tmpBkReader.readedXElement.getSingleText("taxname"+i));
                                            map.put("Taxamount",tmpBkReader.readedXElement.getSingleText("taxamount"+i));
                                            */
                                            list.add(dto);
                                        }
                                    } else {
                                        tmpRetDetail.taxcnt = "0";
                                    }
                                    retAts.writeRetDetail(tmpRetDetail, list);
                                    qryCount++;
                                }
                            } else {
                                tmpRespCode = "-1";
                                if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                                    tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                                }
                                tmpRespInfo = tmprspmsg;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            if (StringHelper.isNullOrEmpty(tmprspmsg)) {
                                tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2eerror-rs/status/rspmsg");
                            }
                            tmpRespInfo = tmprspmsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                if (qryCount == countNum) {
                    break;
                }
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("交易发生异常：" + e);
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
